#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=1e5;
typedef long long LL;
inline LL read(){LL x=0,f=1;char ch=getchar();	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
int main(void)
{
  cin.tie(0);std::ios::sync_with_stdio(false);
  LL n;cin>>n;
  LL ans=0;
  bool flag=1;
  for(LL i=1;i<=n;i++){
     LL x,y;cin>>x>>y;
     if(x==y) continue;
     LL temp1=x;
     LL num13=0;LL num15=0;
     while(temp1%3==0){
        num13++;
        temp1/=3;
     }
     while(temp1%5==0){
        num15++;
        temp1/=5;
     }

     LL temp2=y;
     LL num23=0;LL num25=0;
     while(temp2%3==0){
        num23++;
        temp2/=3;
     }
     while(temp2%5==0){
        num25++;
        temp2/=5;
     }
     if(temp1!=temp2){
        flag=0;break;
     }
     LL res=0;
     if(num15<num25){
        swap(num15,num25);
        swap(num13,num23);
     }
     LL v=max(num15,num25)-min(num15,num25);
     res+=v;
     num15-=v;
     num13+=v;
     res+=( max(num13,num23)-min(num13,num23) ) ;
     ans+=res;
  }
  if(flag==0) cout<<"-1"<<"\n";
  else cout<<ans<<"\n";
return 0;
}
